﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Models;
using BLL;
using MySql.Data.MySqlClient;

namespace LinSM
{
    /// <summary>
    /// 赠送血盟道具
    /// </summary>
    public partial class GiveClan : Form
    {
        public GiveClan()
        {
            InitializeComponent();
        }
        GiveClanManager gcm = new GiveClanManager();
        int clanID = 0;//血盟ID

        /// <summary>
        /// 发送到具
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGive_Click(object sender, EventArgs e)
        {
            if (txtClan.Text == "")
            {
                MessageBox.Show("请选择血盟");
                return;
            }
            if (txtItem.Text == "")
            {
                MessageBox.Show("请输入成员道具");
                txtItem.Select();
                return;
            }
            if (txtCount.Text == "")
            {
                MessageBox.Show("请输入数量");
                txtCount.Select();
                return;
            }
            if (txtLevel.Text == "")
            {
                MessageBox.Show("请输入等级");
                txtLevel.Select();
                return;
            }
            string clan = txtClan.Text.Trim();
            int item = Convert.ToInt32(txtItem.Text.Trim());
            int level = Convert.ToInt32(txtLevel.Text.Trim());
            int count = Convert.ToInt32(txtCount.Text.Trim());
            MessageBox.Show(gcm.AddItem(clanID, item, count, level, 1));
        }

        /// <summary>
        /// 焦点离开后检测道具ID是否存在
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtItem_Leave(object sender, EventArgs e)
        {
            string id = txtItem.Text.Trim();
            if (txtItem.Text == "")
            {
                return;
            }
            if (isInt(id))
            {
                MessageBox.Show("请输入道具的ID");
                txtItem.Select();
                return;
            }
            if (!gcm.QuestItem(id))
            {
                MessageBox.Show("道具ID不存在");
                txtItem.Select();
                return;
            }
            if (gcm.ItemType(Convert.ToInt32(id)) == 0)
            {
                txtCount.Text = "1";
                txtCount.Enabled = false;//不可以重叠的道具不可以修改数量
            }
            else
            {
                txtCount.Enabled = true;
            }
        }

        /// <summary>
        /// 判断道具编号是否为整数
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private bool isInt(string id)
        {
            int x = 0;
            try
            {
                Convert.ToInt32(id);
                x = 1;
            }
            catch (Exception)
            {
            }
            if (x == 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GiveClan_Load(object sender, EventArgs e)
        {
            dgvClan.DataSource = gcm.GetAllClan();
            if (dgvClan.SelectedRows.Count != 0)
            {
                dgvClan.CurrentRow.Selected = false;//不让这个控件有默认选中 
            }
        }
        
        /// <summary>
        /// dgv选择行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvClan_Click(object sender, EventArgs e)
        {
            if (dgvClan.SelectedRows.Count == 0)
            {
                return;
            }
            
            txtClan.Text = dgvClan.CurrentRow.Cells[1].Value.ToString();
            clanID = Convert.ToInt32( dgvClan.CurrentRow.Cells[0].Value);
        }

        private void btnGiveClan_Click(object sender, EventArgs e)
        {
            string itemID = txtCastleItem.Text.Trim();
            if (txtCastleItem.Text == "")
            {
                MessageBox.Show("请输入道具");
                return;
            }
            if (isInt(itemID))
            {
                MessageBox.Show("请输入道具的ID");
                txtItem.Select();
                return;
            }
            if (!gcm.QuestItem(itemID))
            {
                MessageBox.Show("道具ID不存在");
                txtItem.Select();
                return;
            }
            //第一个参数代表城堡的id,则只为统治该城堡的王族赠送
            MessageBox.Show(gcm.AddItem(1, Convert.ToInt32(itemID), 1, 0, 2));

        }
    }
}
